package com.zq.it.action.exam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import com.zq.it.action.BaseAction;
import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.Teacher;
import com.zq.it.pojo.exam.TQuestionDb;
import com.zq.it.pojo.exam.TQuestions;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.tool.Constant;
import com.zq.it.tool.StringUtil;

/**
 * 试题管理及题库管理
 * 
 * @author 王凡
 * @version $ID: QuestionAction.java 2013-8-7 王凡@$
 */
public class QuestionAction extends BaseAction {

	private ObjectData objList;
	private Teacher teacher;
	private TQuestionDb qdb;
	private String[] ids;
	private String skey; // 答案
	private String[] soption; // 选项内容
	private TQuestions question;
	private String qcomplex;
	private List list;

	/**
	 * *******************************************************************
	 * 以下为题库操作相关
	 * *******************************************************************
	 */

	/**
	 * 获取题库列表
	 * 
	 * @return
	 */
	public String getQDBList() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TQuestionDb as t where t.Teacher.id = '"
				+ teacher.getId() + "'");
		config.setCountHql(
				"select count(*) from TQuestionDb as t where t.Teacher.id = '"
						+ teacher.getId() + "'");
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		config.setUrl("questionDb_getQDBList.asp");
		objList = questionService.getQuestionDBList(config);
		return "getQDBList";
	}

	/**
	 * 进入增加试题库的页面
	 * 
	 * @return
	 */
	public String preAddQDB() {
		return "preAddQDB";
	}

	/**
	 * 验证试题库名
	 */
	public void validateAddQDB() {
		if (null == qdb || null == qdb.getDbName()
				|| qdb.getDbName().length() > Constant.LENGTH_20
				|| qdb.getDbName().length() < Constant.LENGTH_1) {
			addFieldError("qdb.dbName", "试题库名称格式不正确！");
		}
	}

	/**
	 * 增加试题库
	 * 
	 * @return
	 * @throws IOException
	 */
	public void addQDB() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		qdb.setCDate(new Date());
		qdb.setTeacher(teacher);
		questionService.addQDB(qdb);
		print_html_utf8("<script> parent.delResult('增加成功！');</script>");
	}

	/**
	 * 删除试题库(未增加验证)
	 * 
	 * @throws IOException
	 * 
	 */
	public void deleteQDB() throws IOException {
		try {
			teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
			print_text_utf8(questionService.deleteQDB(ids, teacher));
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	/**
	 * 进入修改试题库的页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String preEditQDB() throws Exception {
		if (null != ids) {
			qdb = questionService.getQDBById(ids[0]);
			return "preEditQDB";
		} else {
			throw new Exception();
		}
	}

	/**
	 * 验证试题库名
	 */
	public void validateeditQDB() {
		if (null == qdb || null == qdb.getDbName()
				|| qdb.getDbName().length() > Constant.LENGTH_20
				|| qdb.getDbName().length() < Constant.LENGTH_1) {
			addFieldError("qdb.dbName", "试题库名称格式不正确！");
		}
	}

	/**
	 * 修改试题库信息
	 * 
	 * @throws IOException
	 */
	public void editQDB() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		try {
			questionService.updateQDB(teacher, qdb);
			print_html_utf8("<script> parent.delResult('修改成功！');</script>");
		} catch (Exception e) {
			print_html_utf8("<script> parent.delResult('修改失败！');</script>");
		}
	}

	/**
	 * 多条件查询试题库
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchQDB() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		if (!StringUtil.isEmpty(qdb.getDbName())) {
			qdb.setDbName(URLDecoder.decode(qdb.getDbName(), Constant.UTF_8));
		}
		String hql = questionService.creatQDBHql(teacher, qdb);
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("questionDb_searchQDB.asp");
		config.setExtendUrl("qdb.dbName=" + URLEncoder.encode(
				URLEncoder.encode(qdb.getDbName(), Constant.UTF_8),
				Constant.UTF_8));
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		objList = questionService.getQuestionDBList(config);
		return "getQDBList";
	}

	/**
	 * ****************************************************** 以下为试题相关操作
	 * ******************************************************
	 */

	/**
	 * 进入试题列表
	 * 
	 * @return
	 */
	public String getQuestionList() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		PageConfig config = new PageConfig();
		config.setHql("from TQuestions as t where t.Teacher.id = '"
				+ teacher.getId() + "'");
		config.setCountHql(
				"select count(*) from TQuestions as t where t.Teacher.id = '"
						+ teacher.getId() + "'");
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		config.setUrl("question_getQuestionList.asp");

		objList = questionService.getQuestionList2Text(config);
		return "getQuestionList";

	}

	/**
	 * 进入新建试题页面
	 * 
	 * @return
	 */
	public String preAddQuestion() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		list = questionService.getQuestionDBList(teacher);
		return "preAddQuestion";
	}

	/**
	 * 增加试题校验
	 * 
	 */
	public void validateAddQuestion() {
		if (null == question.getQType()) {
			addFieldError("question.content", "创建试题出错！");
			return;
		} else if (question.getTQuestionDb().getId() == 0) {
			addFieldError("question.TQuestionDb.id", "请选择试题库！");
			return;
		} else if (null == question.getContent()
				|| question.getContent().equals("")) {
			addFieldError("question.content", "请输入题干内容！");
			return;
		} else {
			switch (question.getQType()) {
				case Constant.QTYPE_SELECT :
					if (soption.length <= 0) {
						addFieldError("question.content", "创建选项出错！");
					}
					break;
			}
		}
	}

	/**
	 * 增加试题
	 * 
	 * @throws IOException
	 * 
	 */
	public void addQuestion() throws IOException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		boolean flag = questionService.addQuestion(question, skey, soption,
				qcomplex, teacher, getRequest());
		if (flag) {
			print_html_utf8("<script> parent.delResult('创建成功！');</script>");
		} else {
			print_html_utf8("<script> parent.delResult('创建失败！');</script>");
		}
	}

	/**
	 * 多条件查询试题
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchQuestion() throws UnsupportedEncodingException {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		if (!StringUtil.isEmpty(question.getContent())) {
			question.setContent(
					URLDecoder.decode(question.getContent(), Constant.UTF_8));
		}
		if (!StringUtil.isEmpty(question.getTQuestionDb().getDbName())) {
			question.getTQuestionDb().setDbName(URLDecoder.decode(
					question.getTQuestionDb().getDbName(), Constant.UTF_8));
		}
		String hql = questionService.creatQuestionHql(teacher, question);
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("question_searchQuestion.asp");
		config.setExtendUrl("question.content="
				+ URLEncoder.encode(URLEncoder.encode(question.getContent(),
						Constant.UTF_8), Constant.UTF_8)
				+ "&question.TQuestionDb.dbName="
				+ URLEncoder.encode(
						URLEncoder.encode(question.getTQuestionDb().getDbName(),
								Constant.UTF_8),
						Constant.UTF_8)
				+ "&question.QType=" + question.getQType() + "&question.status="
				+ question.getStatus());
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		objList = questionService.getQuestionList2Text(config);
		return "getQuestionList";
	}

	/**
	 * 查看试题详情
	 * 
	 * @return
	 */
	public String showQuestion() {
		teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
		question = questionService.getQuestionById(ids[0], teacher);
		if (null == question) {
			return "ffdsfds";
		}
		switch (question.getQType()) {
			case Constant.QTYPE_SELECT :
				soption = questionService.getOptions(question.getId());
				break;
		}
		return "showQuestion";
	}

	/**
	 * 删除试题(未增加验证)
	 * 
	 * @throws IOException
	 * 
	 */
	public void deleteQuestion() throws IOException {
		try {
			teacher = (Teacher) getHttpSession().getAttribute(Constant.TEACHER);
			print_text_utf8(questionService.deleteQuestion(ids, teacher));
		} catch (Exception e) {
			e.printStackTrace();
			print_text_utf8("false");
		}
	}

	public ObjectData getObjList() {
		return objList;
	}

	public void setObjList(ObjectData objList) {
		this.objList = objList;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public TQuestionDb getQdb() {
		return qdb;
	}

	public void setQdb(TQuestionDb qdb) {
		this.qdb = qdb;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public TQuestions getQuestion() {
		return question;
	}

	public void setQuestion(TQuestions question) {
		this.question = question;
	}

	public String getSkey() {
		return skey;
	}

	public void setSkey(String skey) {
		this.skey = skey;
	}

	public String[] getSoption() {
		return soption;
	}

	public void setSoption(String[] soption) {
		this.soption = soption;
	}

	public String getQcomplex() {
		return qcomplex;
	}

	public void setQcomplex(String qcomplex) {
		this.qcomplex = qcomplex;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
}
